﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using ReporterEngine;
using ReporterSystem;

namespace ReporterControlLibrary.Models
{
    public partial class ReportInfo
    {
        private bool _isLastItem = false;
        public ReportTemplate ReportTemplate { get; set; }

        public bool IsLastItem
        {
            get { return _isLastItem; }
            set
            {
                if (this._isLastItem != value)
                {
                    this._isLastItem = value;
                    this.OnPropertyChanged("IsLastItem");
                }
            }
        }

        private object _templateTreeSource;

        public object TemplateTreeSource
        {
            get { return _templateTreeSource; }
            set
            {
                if (this._templateTreeSource != value)
                {
                    this._templateTreeSource = value;
                    this.OnPropertyChanged("TemplateTreeSource");
                }
            }
        }

        private object _templateTreeInstancesSource;

        public object TemplateTreeInstancesSource
        {
            get { return _templateTreeInstancesSource; }
            set
            {
                if (this._templateTreeInstancesSource != value)
                {
                    this._templateTreeInstancesSource = value;
                    this.OnPropertyChanged("TemplateTreeInstancesSource");
                }
            }
        }

        protected override void OnPropertyChanged(string property)
        {
            base.OnPropertyChanged(property);
            if (property == "ResultName" || property == "TemplateName" || property == "AutoOpenResult")
            {
                if (this.Project != null && !String.IsNullOrEmpty(ResultName) && !String.IsNullOrEmpty(TemplateName))
                {
                    var tpl =
                        this.Project.ReportProject.Templates.Where(
                            x => x.ReportTemplateID == this.ReportTemplate.ReportTemplateID).SingleOrDefault();

                    //update files
                    ReportApp.UpdateTemplate(tpl, this.Project.ReportProject, TemplateName, ResultName);

                    tpl.TemplateFileName = this.TemplateName;
                    tpl.ResultFileName = this.ResultName;
                    tpl.AutoOpenResult = this.AutoOpenResult;

                    //change report and update
                    ReportApp.UpdateProjectInfo(this.Project.ReportProject);
                }
            }
        }

        partial void OnLastReportDateChanged()
        {
            IsActual = LastReportDate.HasValue && this.Project.ProjectInfo.ImportedDate < LastReportDate;
        }

        partial void OnIsUsedForGenerationChanged()
        {
            if (Project != null && IsUsedForGeneration)
                Project.UsedReportInfoId = this.Id;
        }        

        partial void OnResultNameChanged()
        {
            IsValid = !String.IsNullOrEmpty(ResultName) && !String.IsNullOrEmpty(TemplateName);
        }

        partial void OnTemplateNameChanged()
        {
            if (!String.IsNullOrEmpty(TemplateName) && String.IsNullOrEmpty(ResultName))
            {
                ResultName = TemplateName;
            }

            IsValid = !String.IsNullOrEmpty(ResultName) && !String.IsNullOrEmpty(TemplateName);
        }

        partial void OnResultFileChanged()
        {
           // IsValid = !String.IsNullOrEmpty(ResultFile) && !String.IsNullOrEmpty(TemplateFile);
        }

        partial void OnTemplateFileChanged()
        {
           // IsValid = !String.IsNullOrEmpty(ResultFile) && !String.IsNullOrEmpty(TemplateFile);
        }
    }
}
